/*
 * Copyright (C) 2018 Zhengzhou grandtech Technology CO.,LTD.
 * All rights reserved.
 * Official Web Site: http://www.grandtech.com.
 * Developer Web Site: http://open.grandtech.com.
 */

package com.grandtech.oss.api;

import cn.hutool.core.util.StrUtil;
import com.grandtech.oss.common.pojo.RestfulMessage;
import com.grandtech.oss.module.model.OSSInformation;
import com.grandtech.oss.service.OSSInformationService;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/***
 *
 * @since:oss-server 1.0
 * @author <a href="mailto:yanggh@foxmail.com">yanggh@foxmail.com</a> 
 * 2018/09/17 10:30
 */
@RestController
@RequestMapping("/api/v1/oss/sys")
public class SystemApplication extends RootApis {
    /**
     * @apiDefine sys sys（基本信息设置或修改）
     */

    @Autowired
    OSSInformationService ossInformationService;

    /***
     * @api {POST} /api/v1/oss/sys/mergeInformation mergeInformation（基本信息设置或修改）
     * @apiName mergeInformation
     * @apiGroup sys
     * @apiVersion 1.0.0
     * @apiDescription mergeInformation设置或修改基础信息（存储路径、素材下载根目录、nginx日志路径）
     *
     * @apiParam {String} id 编码
     * @apiParam {String} root 存储路径
     * @apiParam {String} invokingRoot 素材下载根目录
     * @apiParam {String} nginxLogPath nginx日志路径
     * @apiParamExample {json} Request-Example1：
     * {
     *     "id": "5b8b7a2d1aa4414f95a2338aba9571d7",
     *     "root": "D:/nginx/html/files/",
     *     "invokingRoot": "http://localhost/files/",
     *     "nginxLogPath": "D:/nginx/logs/access.log"
     * }
     * @apiParamExample {json} Request-Example2：
     * {
     *     "id": "5b8b7a2d1aa4414f95a2338aba9571d7",
     *     "root": "/usr/share/nginx/html/files/",
     *     "invokingRoot": "http://gykj123.cn:9022/files/",
     *     "nginxLogPath": "/usr/local/nginx/logs/access.log"
     * }
     *
     * @apiSuccess {json} success 返回数据
     * @apiSuccessExample {json} Success-Response:
     * {
     *     "code": 8200,
     *     "data": "5b8b7a2d1aa4414f95a2338aba9571d7",
     *     "message": "Success"
     * }
     */
    @PostMapping("/mergeInformation")
    public RestfulMessage mergeInformation(OSSInformation ossInformation) {
        RestfulMessage restfulMessage = new RestfulMessage();
        try {
            assertArgumentNotEmpty(ossInformation.getRoot(), "存储路径不能为空");
            assertArgumentNotEmpty(ossInformation.getInvokingRoot(), "素材下载根目录不能为空");
            isTrue(ossInformation.getRoot().endsWith("/"), "存储路径必须以/结尾");
            if (StrUtil.isEmpty(ossInformation.getId())) {
                ossInformation.setCreateTime(DateTime.now().toString("yyyy-MM-dd HH:mm:ss"));
                ossInformationService.insert(ossInformation);
            } else {
                ossInformation.setModifiedTime(DateTime.now().toString("yyyy-MM-dd HH:mm:ss"));
                ossInformationService.updateById(ossInformation);
            }
            restfulMessage.setData(ossInformation.getId());
            successResultCode(restfulMessage);
        } catch (Exception e) {
            restfulMessage = wrapperException(e);
        }
        return restfulMessage;
    }
}
